

use std::fs::remove_dir_all;

use clap::Args;
use serde::{Deserialize, Serialize};
use wtask_base::{app::get_wtask_result_dir, error::{WError, WResult}};

use crate::{task::utils::func::archv_task_result, utils::AppTrait};




#[derive(Debug, Clone, Serialize, Deserialize, Args)]
pub struct AppTaskArchv {
    /// 是否执行删除操作，默认操作存档
    #[arg(long)]
    pub del: bool,

    /// 任务运行ID
    pub id: String,
}


impl AppTrait for AppTaskArchv {
    type Status = ();
    type Res = String;

    async fn server_handle_single(self) -> WResult<String> {
        if self.del {
            let dir_task = get_wtask_result_dir(&self.id)?;
            if let Some(dir_task) = dir_task {
                let _ = remove_dir_all(dir_task);
            }
            Ok(format!("Task result del: {}", self.id))
        } else {
            let dir_task = get_wtask_result_dir(&self.id)?.ok_or(WError::Empty)?;
            let dir_res = archv_task_result(&dir_task)?;
            Ok(dir_res.to_string_lossy().to_string())
        }
    }
}

